package com.kucai.yifaqapp.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kucai.yifaqapp.R;
import com.kucai.yifaqapp.dagger.component.fragment.DaggerWithdrawalFragmentComponent;
import com.kucai.yifaqapp.dagger.module.fragment.WithdrawalFragmentModule;
import com.kucai.yifaqapp.global.Info;
import com.kucai.yifaqapp.model.network.entity.res.Pay_QukuanList;
import com.kucai.yifaqapp.model.network.entity.res.z_outmoneyDetail;
import com.kucai.yifaqapp.presenter.fragment.WithdrawalFragmentPresenter;
import com.kucai.yifaqapp.ui.fragment.gameFragment.MoneyOutdetailDialog;
import com.kucai.yifaqapp.ui.view.WithdrawalFragmentView;
import com.kucai.yifaqapp.util.OnItemClickListener;

import butterknife.BindView;

/**
 * 取款记录
 */
public class WithdrawalFragment extends BaseFragment<WithdrawalFragmentPresenter> implements WithdrawalFragmentView {
    @BindView(R.id.withdrawl_rl)
    RecyclerView withdrawlRl;
    private MyAdapter activtyAdapter;
    private Pay_QukuanList mDatas;

    @Override
    protected void ChangeSkin() {

    }

    @Override
    protected int setContentView() {
        return R.layout.fragment_withdrawal;
    }

    @Override
    protected void initDagger() {
        DaggerWithdrawalFragmentComponent.builder()
                .withdrawalFragmentModule(new WithdrawalFragmentModule(this))
                .build().inject(this);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        InitData();
        InitView();
    }

    private void InitView() {
        //活动列表view
        //添加布局管理器
        withdrawlRl.setLayoutManager(new LinearLayoutManager(getContext()));
//        homeRl.setLayoutManager(linearLayoutManager);
        //创建适配器
        activtyAdapter = new MyAdapter();
    }

    private void InitData() {
        if(Info.user!=null){
            mPresenter.getQukuanList(Info.user.token);
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(!hidden){
            //刷新操作
            if(Info.user!=null){
                mPresenter.getQukuanList(Info.user.token);
            }
        }
    }

    @Override
    public void getQukuanList(Pay_QukuanList list) {
        mDatas = list;
        if(mDatas!=null){
            if(mDatas.getData()!=null){
                withdrawlRl.setAdapter(activtyAdapter);
            }
        }
    }

    /////////////////////////////////////////////////////////////////////////////////////////////////
    class MyAdapter extends RecyclerView.Adapter<MyAdapter.MViewHolder>
            implements OnItemClickListener {
        @Override
        public MViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MViewHolder(LayoutInflater.from(getContext())
                    .inflate(R.layout.deposite_item, parent, false));

        }

        @Override
        public void onBindViewHolder(final MViewHolder holder, final int position) {
            try {
                holder.time_tv.setText(mDatas.getData().get(position).getApplyTime());
                holder.rechmoney.setText(mDatas.getData().get(position).getApplyMoney());
//                checkStatus：0未受理 1处理中 2通过 3 不通过 4 锁定
                switch (mDatas.getData().get(position).getCheckStatus()) {
                    case 0:
                        holder.states.setText("未受理");
                        break;
                    case 1:
                        holder.states.setText("处理中");
                        break;
                    case 2:
                        holder.states.setText("通过");
                        break;
                    case 3:
                        holder.states.setText("不通过");
                        break;
                    case 4:
                        holder.states.setText("锁定");
                        break;
                }
                holder.itemclick.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        z_outmoneyDetail.bean=mDatas.getData().get(position);
                        startActivity(new Intent(getActivity(),MoneyOutdetailDialog.class));
                    }
                });
            } catch (Exception e) {

            }
        }

        @Override
        public int getItemCount() {
            return mDatas.getData().size();
        }

        @Override
        public void onItemClick(View view, int position) {

        }

        class MViewHolder extends RecyclerView.ViewHolder {
            TextView time_tv;
            TextView rechmoney;
            TextView states;
            LinearLayout itemclick;
            MViewHolder(View itemView) {
                super(itemView);
                time_tv = (TextView) itemView.findViewById(R.id.time_tv);
                rechmoney = itemView.findViewById(R.id.rechmoney);
                states = itemView.findViewById(R.id.states);
                itemclick=itemView.findViewById(R.id.itemclick);
            }
        }
    }
}
